import urllib.request
import http.client
import json
import config
import ssl


class BmobHelper(object):
    def __init__(self):
        self.requrl = config.REQURL  # 替换为自己的表名
        self.headerdata = config.HEADERDATA

    def record_op(self, op_type, value):
        context = ssl._create_unverified_context()
        conn = http.client.HTTPSConnection("api.bmob.cn", context=context)
        data = {'op_type': op_type, 'value': value};
        data = json.dumps(data)
        if op_type == 'profit':
            print("%s: %s%s" % (op_type, value, "btc"))
        else:
            print("%s: %s%s" % (op_type, value, "%"))
        try:
            conn.request(method="POST", url=self.requrl, body=data, headers=self.headerdata)
        except Exception as e:
            print(e)
            print("Add data error!")

        conn.close()
